package login.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import commons.pojo.Users;
import login.dao.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MyDetailsService implements UserDetailsService {
    @Autowired
    private LoginMapper loginMapper;

     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
         // 1.构造查询条件
         System.out.println(username);
         // 2.查询用户
//         Users users = loginMapper.selectById(username);
         QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>().eq("name", username);
         Users users = loginMapper.selectOne(queryWrapper);
//         // 3.封装为UserDetails对象
//         UserDetails userDetails = User.withUsername(users.getName())
//                 .password(users.getPassword())
//                 .authorities("admin").build();
         String password = users.getPassword();
//         PasswordEncoder encoder = new BCryptPasswordEncoder();
//         if(encoder.matches())

         ArrayList<GrantedAuthority> authorities = new ArrayList<>();
         UserDetails user = new User(username,password,true,true,true,true,authorities);
         // 4.返回封装好的UserDetails对象
         return user;
     }
}
